<template>
	<view class="main">
		<view class="active_tit">
			<view class="tit">
				个人资产情况（房产类/土地类）
			</view>
			<!-- <view class="add" @click="add">
				＋
			</view> -->
			<lb-picker :props='props0' @confirm='confirm0' ref='Picker0' :list='list0'></lb-picker>
			<lb-picker :props='props' @confirm='confirm' ref='Picker1' :list='list'></lb-picker>
			<lb-picker :props='props2' @confirm='confirm2' ref='Picker2' :list='list2'></lb-picker>
		</view>
		<!-- input -->
		<block v-for="(item,index) in stdOffsurCollateral01" :key='index'>
			<view class="cut">房产{{index+1}} 【为避免楼栋号，房号输入不准确，请点击【精准选择】按钮选择确认。】 <uni-icons class="clear" type="clear" @click='stdDel(stdOffsurCollateral01,item,"stdOffsurCollateral")'></uni-icons>
			</view>
		
			<ActiveForm required ref=''>
				<view class="active_label">
					房产性质：
				</view>
				<view class="active_input">
					<SelectPicker v-model="item.buildingAttr" :options="buildingAttrOption" placeholder="请选择" />
				</view>
			</ActiveForm>
			
			<ActiveForm required ref='mortgageAdd'>
				<view class="active_label">
					资产所在地区：
				</view>
				<view class="active_input">
					<RegionPicker :value="mortgageAdd(item.mortgageAdd)" :options="region2" @change="((o)=>{item.mortgageAdd=o.value.toString()})" />
					<!-- <input type="text" v-model="form.mortgageAdd" /> -->
				</view>
			</ActiveForm>

			<ActiveForm required ref='buildingName'>
				<view class="active_label">
					楼盘名称：
				</view>
				<view class="active_input">
					<input type="text" v-model="item.buildingName" placeholder="关键字查询" />
					<button type="pg" @click="selectXx(index,'buildingName',0)">楼盘查询</button>
				</view>
			</ActiveForm>
			<ActiveForm required ref='buildingNo'>
				<view class="active_label">
					楼栋号（号数）：
				</view>
				<view class="active_input">
					<input type="text" :disabled='zcDisabled' v-model="item.buildingNo" />
					<button type="pg" @click="selectXx(index,'buildingNo',1)">精准选择</button>
				</view>
			</ActiveForm>
			<ActiveForm required ref='floor'>
				<view class="active_label">
					所在楼层（层数）：
				</view>
				<view class="active_input">
					<input @input="fcInout(item)" type="number" v-model="item.floor" />

				</view>
			</ActiveForm>
			<ActiveForm required ref='floor'>
				<view class="active_label">
					房号（号数）：
				</view>
				<view class="active_input">
					<input :disabled='zcDisabled' type="text" v-model="item.roomNo" />
					<button type="pg" @click="selectXx(index,'roomNo',2)">精准选择</button>
				</view>
			</ActiveForm>
			<ActiveForm required ref='allFloor'>
				<view class="active_label">
					总层数（层数）：
				</view>
				<view class="active_input">
					<mInput :thousand='false' type="number" v-model="item.allFloor" />
				</view>
			</ActiveForm>
			<ActiveForm required ref='area'>
				<view class="active_label">
					面积（平方米）：
				</view>
				<view class="active_input">
					<mInput @input="fcInout(item)" :thousand='false' type="number" v-model="item.area" />
				</view>
			</ActiveForm>
			<ActiveForm required ref='guaranteeBalance'>
				<view class="active_label">
					贷款余额（元）：
				</view>
				<view class="active_input">
					<mInput placeholder='此套房产的所有抵押贷款' maxlength="16" type="number" v-model="item.guaranteeBalance" />
				</view>
			</ActiveForm>
			<ActiveForm required ref='autoAssessPrice'>
				<view class="active_label">
					评估价值（线上）：
				</view>
				<view class="active_input">
					<mInput type="number" :disabled="true" v-model="item.autoAssessPrice" />
					<button type="pg" @click="pg(item,index)">线上评估</button>
				</view>
			</ActiveForm>
			<ActiveForm required ref='manAssessPrice'>
				<view class="active_label">
					评估价值（人工）：
				</view>
				<view class="active_input">
					<mInput maxlength="16" type="number" v-model="item.manAssessPrice" />
				</view>
			</ActiveForm>
			<ActiveForm required ref='residue'>
				<view class="active_label">
					剩余价值（元）：
				</view>
				<view class="active_input">
					<mInput type="number" :disabled="true" :value="sy(item)" />
				</view>
			</ActiveForm>
			<ActiveForm  ref=''>
				<view class="active_label">
					备注：
				</view>
				<view class="active_input">
					<input type="text" v-model="item.remark" />
				</view>
			</ActiveForm>
			<block v-for="(zitem,index) in item.obligeeNameList">
				<view class="cut">权利人{{index+1}}
					<uni-icons class="clear" type="clear" @click='stdDel(item.obligeeNameList,zitem,"stdOffsurCollateralPledgor")'></uni-icons>
				</view>
				
				<ActiveForm required ref=''>
					<view class="active_label">
						权利人名称：
					</view>
					<view class="active_input">
						<input type="text" v-model="zitem.pledgorName" />
					</view>
				</ActiveForm>
				
			</block>
			<view class="active_tit">
				<view class="tit">
				</view>
				<view class="add" @click="addUser(index)">
					添加权利人
				</view>
			</view>
		</block>

		<view class="active_tit">
			<view class="" style="display: flex;">
				<view class="add" @click="add">
					添加资产
				</view>

			</view>
		</view>
	</view>
</template>

<script>
	import SelectPicker from "../../SelectPicker";
	import RegionPicker from "../../RegionPicker";
	import region from '../areaList.js'
	export default {
		name: 'userAssets',
		data() {
			return {
				loanType:'',
				region,
				region2: [],
				province: "",
				city: "",
				county: "",
				index: 1,
				stdOffsurCollateral01: [],
				buildingAttrOption: [],
				pledgorTypeOption: [],
				idenTypeOption: [],
				pledgorIdenAttOption: [],
				selectIndex: null,
				selectName: '',
				props0: {
					label: 'projectName',
					value: 'projectId'
				},
				props: {
					label: 'buildingName',
					value: 'buildingName'
				},
				props2: {
					label: 'houseName',
					value: 'houseName'
				},
				list: [],
				list0: [],
				list2: [],
				zcDisabled: true
			}
		},
		components: {
			SelectPicker,
			RegionPicker,

		},
		computed: {
			sy() {
				return function(item) {
					if(this.loanType==='4'){
						item.residue = item.manAssessPrice - item.guaranteeBalance
						
					}else{
						item.residue = item.autoAssessPrice != '' && item.autoAssessPrice != '0' ? item.autoAssessPrice - item.guaranteeBalance :
							item.manAssessPrice - item.guaranteeBalance
					}
					
					if(item.residue<0){
						return 0
					}else{
						return item.residue
					}
				}
			},
			mortgageAdd() {
				return function(val) {
					if (val == '' || val == null) {
						return []
					} else {
						let arr = val.split(",")
						return arr
					}
				}
			}
		},
		mounted() {
			//获取返显数据
			setTimeout(() => {
				//数据反显
				this.region2 = getApp().globalData.FxtData
                this.loanType = getApp().globalData.stdOffsurBaiscInfo.loanType
				this.buildingAttrOption = getApp().globalData.optType.BUL_ATT
				this.pledgorTypeOption = getApp().globalData.optType.SUBJECT
				this.idenTypeOption = getApp().globalData.optType.PLEDGOR_IDEN
				this.pledgorIdenAttOption = getApp().globalData.optType.PLEDGOR_IDEN_ATT
				let arr = getApp().globalData.stdOffsurCollateral.dyws
				if (arr != null) {
					for (let i = 0; i < arr.length; i++) {
						if (arr[i].busiType == '01') {
							this.stdOffsurCollateral01.push(arr[i])
						}
					}
				}
			}, getApp().globalData.timeout)
		},
		watch: {

			stdOffsurCollateral01: {
				handler: function(res) {
					//console.warn('stdOffsurCollateral01组件数据变化',res)
					getApp().globalData.stdOffsurCollateral01 = res
				},
				deep: true
			}
		},
		methods: {
			fcInout(item){
				
				item.autoAssessPrice = 0
			},
			selectXx(index, name, type) {

				this.selectIndex = index
				this.selectName = name

				//接口评估房产
				let item = this.stdOffsurCollateral01[index]
				let arr = item.mortgageAdd.split(',')
				//console.log(arr)
				if (type == 0) {
					if (item.mortgageAdd == '') {
						uni.showToast({
							icon: 'none',
							title: '请先选择资产所在地',
							duration: 2000
						})
						return false
					}
					if (item.buildingName == '') {
						uni.showToast({
							icon: 'none',
							title: '请先输入楼盘关键字',
							duration: 2000
						})
						return false
					}

					let data = {
						"areaId": arr[2],
						"cityId": arr[1],
						"key": item.buildingName
					}
					this.sendRequest({
						url: 'fxt/project',
						method: 'post',
						hideLoading: true,
						data: data,
						complete: (res) => {
							if (res.code == '0') {
								this.list0 = res.body
								this.$refs.Picker0.show() // 显示
							} else {
								//this.zcDisabled = false
								uni.showToast({
									icon: 'none',
									title: '楼盘获取失败，请重试',
									duration: 2000
								})
							}

						}
					})

				} else if (type == 1) {
					if (item.mortgageAdd == '') {
						uni.showToast({
							icon: 'none',
							title: '请先选择资产所在地',
							duration: 2000
						})
						return false
					}
					if (item.buildingName == '') {
						uni.showToast({
							icon: 'none',
							title: '请先输入楼盘名称',
							duration: 2000
						})
						return false
					}
					let data = {
						"areaId": arr[2],
						"buildingName": item.buildingNo,
						"cityId": arr[1],
						"floor": item.floor,
						"houseName": item.roomNo,
						"projectName": item.buildingName,
						"projectId": item.projectId
					}
					this.sendRequest({
						url: 'fxt/building',
						method: 'post',
						hideLoading: true,
						data: data,
						complete: (res) => {
							if (res.code == '0') {
								this.list = res.body
								this.$refs.Picker1.show() // 显示
							} else {
								//this.zcDisabled = false
								uni.showToast({
									icon: 'none',
									title: '楼栋号获取失败,请重试',
									duration: 2000
								})
							}

						}
					})
				} else {
					if (!item.buildingId) {
						uni.showToast({
							icon: 'none',
							title: '请先精准获取楼栋号',
							duration: 2000
						})
						return false
					}
					if (!item.floor) {
						uni.showToast({
							icon: 'none',
							title: '请输入楼层',
							duration: 2000
						})
						return false
					}
					let data = {
						"cityId": arr[1],
						"buildingId": item.buildingId,
						"floor": item.floor,
						"areaId": arr[2],
						"buildingName": item.buildingNo,
					}
					this.sendRequest({
						url: 'fxt/house',
						method: 'post',
						hideLoading: true,
						data: data,
						complete: (res) => {
							if (res.code == '0') {
								this.list2 = res.body
								this.$refs.Picker2.show() // 显示
							} else {
								//this.zcDisabled = false
								uni.showToast({
									icon: 'none',
									title: '房号获取失败,请重试',
									duration: 2000
								})
							}

						}
					})
				}



				//this.$refs.picker.hide() // 隐藏
			},
			confirm0(e) {
				console.log(e)
				this.stdOffsurCollateral01[this.selectIndex][this.selectName] = e.item.projectName
				this.stdOffsurCollateral01[this.selectIndex].projectId = e.item.projectId
			},
			confirm(e) {

				this.stdOffsurCollateral01[this.selectIndex].projectId = this.list[e.index].projectId
				this.stdOffsurCollateral01[this.selectIndex].buildingId = this.list[e.index].buildingId
				this.stdOffsurCollateral01[this.selectIndex][this.selectName] = e.value
			},
			confirm2(e) {
				this.stdOffsurCollateral01[this.selectIndex].houseId = this.list2[e.index].houseId
				this.stdOffsurCollateral01[this.selectIndex][this.selectName] = e.value
			},
			addUser(index) {
				this.stdOffsurCollateral01[index].obligeeNameList.push({
					idenCode: '', //证件号码
					idenType: '', //证件类型
					pledgorIdenAtt: '', //抵押人身份属性
					pledgorName: '', //抵押人名称
					pledgorType: '', //抵押人类型
					postCode: '', //邮政编码
					residence: '', //住所
					telephone: '', //联系电话
					tranAdd: '' //通讯地址
				})
			},
			wyjy(item, index) {
				//接口评估房产
				let data = {
					"idnfNo": item.certificateCode,
					"collatType": "1"
				}
				this.sendRequest({
					url: 'esbCollateral/collateralIsUnique',
					method: 'post',
					hideLoading: true,
					data: data,
					complete: (res) => {

						if (res.data.exstStatus == '0') {
							uni.showToast({
								icon: 'none',
								title: '验证通过',
								duration: 2000
							})
						} else {
							uni.showToast({
								icon: 'none',
								title: '验证失败',
								duration: 2000
							})
						}
					}
				})
			},
			pg(item, index) {
				//接口评估房产
				let arr = item.mortgageAdd.split(',')
				let data = {
					"areaId": arr[2],
					"buildingName": item.buildingNo,
					"cityId": arr[1],
					"floor": item.floor,
					"houseName": item.roomNo,
					"projectName": item.buildingName,
					buildArea: item.area,
					projectId: item.projectId,
					buildingId: item.buildingId,
					houseId: item.buildingId
				}
				this.sendRequest({
					url: 'fxt/enquiry',
					method: 'post',
					hideLoading: true,
					data: data,
					complete: (res) => {

						if (res.code == '0') {
							item.autoAssessPrice = res.body.totalPrice
						} else {
							uni.showToast({
								icon: 'none',
								title: res.message || '评估失败',
								duration: 2000
							})
						}
					}
				})
			},
			add() {
				let obj = {
					mortagageCount: '',
					allFloor: '', //总层数
					area: '', //面积
					autoAssessPrice: '', //系统评估价值
					buildingNo: '',
					buildingAttr: "", //房产性质
					buildingName: "", //楼盘名称
					busiType: "01", //业务类型
					businessId: "", //业务申请流水号
					certificateAdd: "", //房产证地址
					certificateCode: "", //房产证号码
					certificateEndDate: "", //土地使用权终止日期
					certificateRegDate: "", //房屋产权登记日期
					collateralType: "", //押品类型
					consignor: "", //委托人
					consignorPhone: "", //委托人电话
					createTime: "", //创建时间
					createUser: "", //创建人
					customerId: "", //客户编号
					customerSystemId: "", //客户系统编号
					deleteFlag: "", //删除标志
					entId: "", //企业ID
					entityRoleType: "", //数据归属类型
					fixStatus: "", //装修状态
					floor: '', //所在楼层
					getDatTime: "", //数据加载时间
					guaranteeBalance: '', //担保余额（元）
					idCode: "", //证件号码
					imageUrl: "", //影像路径
					manAssessPrice: '', //人工评估（抵押、质押）价值
					mortgageAdd: '', //抵押物所在地区
					obligeeName: "", //权利人名称
					obligeeNameList: [{
							idenCode: '', //证件号码
							idenType: '', //证件类型
							pledgorIdenAtt: '', //抵押人身份属性
							pledgorName: '', //抵押人名称
							pledgorType: '', //抵押人类型
							postCode: '', //邮政编码
							residence: '', //住所
							telephone: '', //联系电话
							tranAdd: '' //通讯地址
						},

					], //多个权利人名称
					pledgeDetail: "", //质押物的具体描述
					reqId: "", //业务申请序号
					residue: '', //剩余价值
					roomNo: '',
					sourceSystem: "", //数据来源系统
					updateTime: "", //更新时间
					updateUser: "", //更新人
					uuid: "", //uuid
					xh: ''
				}
				this.stdOffsurCollateral01.push(obj)
			}
		}
	}
</script>

<style lang="scss">


</style>
